/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.web.controller;

import com.bifrost.annotation.CurrentStore;
import com.bifrost.common.annotation.Store;
import com.bifrost.common.util.HttpRequestUtils;
import com.bifrost.entity.IndexSetting;
import com.bifrost.entity.IndexSettingRegion;
import com.bifrost.entity.Media;
import com.bifrost.entity.MerchantStore;
import com.bifrost.model.IndexConfigModel;
import com.bifrost.common.model.ResultModel;
import com.bifrost.service.IndexService;
import com.bifrost.service.IndexSettingRegionService;
import com.bifrost.service.StoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author joewee
 * @version 1.0.0
 * @Date 2017/10/20 11:02
 */
@RestController
@RequestMapping("/api/index")
@Api("首页配置接口")
public class IndexController {
    @Autowired
    IndexSettingRegionService indexSettingRegionService;

    @Autowired
    StoreService storeService;

    @Autowired
    IndexService indexService;

    @GetMapping("/config")
    @ApiOperation("获取指定门店首页配置信息")
    @Store
    public ResultModel getIndexConfig(@CurrentStore Long storeId) {
        ResultModel result = indexService.getIndexConfig(storeId);
        if (result != null) return result;
        return ResultModel.error("没有满足条件的数据");
    }

    @Nullable
    private ResultModel getResultModel(MerchantStore store, List<IndexSettingRegion> list) {
        if (list != null) {
            List<IndexConfigModel> result = new ArrayList<IndexConfigModel>(list.size());
            for (IndexSettingRegion indexSettingRegion : list) {
                Media media = indexSettingRegion.getIndexRegionImg();
                String title = indexSettingRegion.getIndexRegionTitle();
                List<IndexSetting> settings = indexSettingRegion.getIndexSettings();
                settings = settings.stream().filter(s->s.getIsVisible().equals(Boolean.TRUE)).collect(Collectors.toList());
                String img = media==null?"":HttpRequestUtils.getUrl(media.getUrl());
                IndexConfigModel model = new IndexConfigModel();
                model.setCode(indexSettingRegion.getTypeCode());
                model.setImg(img);
                model.setTitle(StringUtils.isBlank(title) ? "" : title);
                model.setTemplate(indexSettingRegion.getTemplateType());
                model.setOrder(indexSettingRegion.getSortOrder());
                List indexSettings = new ArrayList<Map>(settings.size());
                model.setItems(indexSettings);
                settings = settings.stream().sorted(Comparator.comparing(IndexSetting::getSortOrder)).collect(Collectors.toList());
                for (IndexSetting indexSetting : settings) {
                    Media settingMedia = indexSetting.getIndexSettingImg();
                    String imgUrl = settingMedia==null?"":HttpRequestUtils.getUrl(settingMedia.getUrl());
                    String url = indexSetting.getIndexSettingUrl();
                    String name = indexSetting.getIndexSettingName();
                    Map item = new HashMap();
                    item.put("img", imgUrl);
                    item.put("url", StringUtils.isBlank(url) ? "#" : url);
                    item.put("title", StringUtils.isBlank(name) ? "" : name);
                    indexSettings.add(item);
                }
                result.add(model);
            }
            return ResultModel.ok()
                    .put("config", result)
                    .put("title", store.getStoreName());
        }
        return null;
    }

    @GetMapping("/config/all")
    @ApiOperation("获取所有门店首页配置信息")
    public ResultModel getIndexConfig() {
        List<IndexSettingRegion> list = indexSettingRegionService.findAllVisible();
        if (list != null) {
            List<IndexConfigModel> result = new ArrayList<IndexConfigModel>(list.size());
            for (IndexSettingRegion indexSettingRegion : list) {
                String title = indexSettingRegion.getIndexRegionTitle();
                Media media = indexSettingRegion.getIndexRegionImg();
                List<IndexSetting> settings = indexSettingRegion.getIndexSettings();
                settings = settings.stream().filter(s->s.getIsVisible().equals(Boolean.TRUE)).collect(Collectors.toList());
                String img = media == null ? "" : HttpRequestUtils.getUrl(media.getUrl());
                IndexConfigModel model = new IndexConfigModel();
                model.setCode(indexSettingRegion.getTypeCode());
                model.setImg(img);
                model.setOrder(indexSettingRegion.getSortOrder());
                model.setTitle(StringUtils.isBlank(title) ? "" : title);
                model.setTemplate(indexSettingRegion.getTemplateType());
                List indexSettings = new ArrayList<Map>(settings.size());
                model.setItems(indexSettings);
                settings = settings.stream().sorted(Comparator.comparing(IndexSetting::getSortOrder)).collect(Collectors.toList());
                for (IndexSetting indexSetting : settings) {
                    Media settingMedia = indexSetting.getIndexSettingImg();
                    String url = indexSetting.getIndexSettingUrl();
                    String imgUrl = settingMedia == null ? "" : HttpRequestUtils.getUrl(settingMedia.getUrl());
                    String name = indexSetting.getIndexSettingName();
                    Map item = new HashMap();
                    item.put("img", imgUrl);
                    item.put("title", StringUtils.isBlank(name) ? "" : name);
                    item.put("url", StringUtils.isBlank(url) ? "#" : url);
                    indexSettings.add(item);
                }
                result.add(model);
            }
            return ResultModel.ok().put("config", result);
        }
        return ResultModel.error("没有满足条件的数据");
    }
    @GetMapping("/config/merchantId")
    @ApiOperation("获取所有门店首页配置信息")
    public ResultModel getIndexConfigFromId(Long merchantId){
        MerchantStore merchantStore = storeService.getMerchantStore(merchantId);
        List<IndexSettingRegion> list = indexSettingRegionService.findAllVisibleByStore(merchantStore);
        ResultModel result = getResultModel(merchantStore, list);
        if (result != null) return result;
        return ResultModel.error("没有满足条件的数据");
    }
}



